package com.astar.service.impl;

import cn.hutool.core.date.DateUtil;
import com.aliyun.oss.*;
import com.aliyun.oss.model.PutObjectRequest;
import com.astar.service.FileTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
public class FileToolsImpl  {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName.image}")
    private String imageBucketName;

    @Value("${aliyun.oss.bucketName.pdf}")
    private String pdfBucketName;

    @Value("${aliyun.oss.bucketName.video}")
    private String videoBucketName;

    @Value("${aliyun.oss.folder}")
    private String folder;

    /**
     * 根据原文件名生成新的随机文件名
     * @param fileName
     * @return
     */
    private String formatFileName(String fileName){
        //路径规则： yyyyMMdd_uuid.文件后缀名
        String newFileName =
                DateUtil.format(new Date(),"yyyyMMdd")
                        +"-"
                        + UUID.randomUUID().toString()
                        + fileName.substring(fileName.lastIndexOf("."));
        return newFileName;
    }


    private String getFileUrl(String bucketName,String endpoint,String fileName){
        StringBuffer stringBuffer = new StringBuffer("https://");
        stringBuffer.append(bucketName);
        stringBuffer.append(".");
        stringBuffer.append(endpoint);
        stringBuffer.append("/");
        stringBuffer.append(fileName);
        return stringBuffer.toString();
    }

    //图片
    public String uploadImage(MultipartFile file) throws IOException {
        return uploadFile(file,imageBucketName);
    }

    //pdf
    public String uploadPdf(MultipartFile file) throws IOException {

        return uploadFile(file,pdfBucketName);
    }

    //视频
    public String uploadVideo(MultipartFile file) throws IOException {
        return uploadFile(file,videoBucketName);
    }


    public String uploadFile(MultipartFile file,String bucketName) throws IOException {

        String fileUrl = null;

        //获取上传图片的文件名
        String fileName = folder+"/"+formatFileName(file.getOriginalFilename());

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
            // 上传文件流。
            ossClient.putObject(bucketName,fileName,file.getInputStream());
            fileUrl = getFileUrl(bucketName,endpoint,fileName);

        return fileUrl;
    }







































//    @Override
//    public String image(File file, String bucketName, String key) {
//        String endpoint = "oss-cn-beijing.aliyuncs.com";
//        String accessKeyId = "LTAI4Fh1XpP6hhd98kaw1QHh";
//        String accessKeySecret = "0MwW47GHJdRs6cLcHItrqrfgKpbZcF";
//
//
//        //创建OSSClient实例
//        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
//
//
//        //创建PutObjectRequest对象
//        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,key,file);
//
//        //上传文件
//        ossClient.putObject(putObjectRequest);
//
//        //关闭OSSClient
//        ossClient.shutdown();
//
//        return "https://"+bucketName+"."+endpoint+"/"+key;
//    }
}
